package com.hp.controller;

import com.hp.pojo.Para;
import com.hp.service.ParaService;
import com.hp.util.PageResult;
import com.hp.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/tpPara")
public class ParaController {

    @Resource
    private ParaService paraService;

    @RequestMapping("/paraIndex")
    public String paraIndex(int templateId, HttpSession session){
        session.setAttribute("templateId",templateId);
        return "para";
    }

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult<Para> findParaPage(int pageNum, int pageSize, @RequestBody Para para,HttpSession session){
        para.setTemplateId((Integer) session.getAttribute("templateId"));
        PageResult pageResult = paraService.findParaPage(pageNum, pageSize,para);
        return pageResult;
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Para para,HttpSession session){
        para.setTemplateId((Integer) session.getAttribute("templateId"));
        try {
            paraService.addPara(para);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody Para para, HttpSession session){
       para.setTemplateId((Integer) session.getAttribute("templateId"));
        try {
            paraService.updatePara(para);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @GetMapping("/findById")
    @ResponseBody
    public Para findById(int id){
        Para byId = paraService.findPara(id);
        return byId;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Result delete(int id){
        try {
            paraService.deletePara(id);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
}
